package com.betterwood.dragon.service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.betterwood.dragon.dao.KeywordDao;
import com.betterwood.dragon.dao.SiteDao;
import com.betterwood.dragon.entity.Keyword;
import com.betterwood.dragon.entity.Site;
import com.betterwood.dragon.utils.TimeUtil;
import com.betterwood.dragon.worms.BLY;
import com.betterwood.dragon.worms.DZ;
import com.betterwood.dragon.worms.FJ;
import com.betterwood.dragon.worms.JF;
import com.betterwood.dragon.worms.JZT;
import com.betterwood.dragon.worms.RY;
import com.betterwood.dragon.worms.YF;
import com.betterwood.dragon.worms.ZK;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice.Local;

import static org.joor.Reflect.*;

@Service(value = "wormService")
@RequiredArgsConstructor
@Slf4j
public class WormService {
    private final KeywordDao keywordDao;
    private final SiteDao siteDao;
    private final WormNest wormNest;

    @Async
    public void autoLogin(String id) {
        Site site = siteDao.getOneById(id);
        if (ObjectUtil.isNull(site)) {
            log.info("网站不存在");
            return;
        }

        // wormNest.getLoginInfoA(site);
        String siteNo = site.getSiteNo();
        String className = "com.betterwood.dragon.worms." + siteNo;
        String res = onClass(className).create().call("login", site).get();
        site.setLoginInfo(res);
        site.setLastLoginTime(LocalDateTime.now());
        siteDao.save(site);

    }

    @Async
    public void JfLogin(String id) {
        Site site = siteDao.getOneById(id);
        if (ObjectUtil.isNull(site)) {
            log.info("网站不存在");
            return;
        }

        // wormNest.getLoginInfoA(site);
        String siteNo = "JF";
        String className = "com.betterwood.dragon.worms." + siteNo;
        String res = onClass(className).create().call("login", site).get();
        site.setLoginInfo(res);
        site.setLastLoginTime(LocalDateTime.now());
        siteDao.save(site);

    }

    @Async
    public void singleLogin(String id) {
        Site site = siteDao.getOneById(id);
        if (ObjectUtil.isNull(site)) {
            log.info("网站不存在");
            return;
        }

        // wormNest.getLoginInfoA(site);
        log.info(site.toString());
        String res = JF.login(site);
        site.setLoginInfo(res);
        site.setLastLoginTime(LocalDateTime.now());
        siteDao.save(site);

    }

}